package com.awifi.demo.netty.chat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/9/14 9:36
 */
public class NIOChatClient {

    private final String HOST = "127.0.0.1";
    private final int PORT = 6667;
    private Selector selector;
    private SocketChannel socketChannel;
    private String username;

    public NIOChatClient() {
        try {
            selector = Selector.open();
            InetSocketAddress inetSocketAddress = new InetSocketAddress(HOST, PORT);
            socketChannel = SocketChannel.open(inetSocketAddress);
            socketChannel.configureBlocking(false);

            socketChannel.register(selector,SelectionKey.OP_READ);
            username = socketChannel.getLocalAddress().toString().substring(1);
            System.out.println(username + "is ok!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(String msg) throws IOException {
        msg = username + " say:" + msg;
        socketChannel.write(ByteBuffer.wrap(msg.getBytes()));
    }

    public void readMsg() throws IOException {
        int select = selector.select();
        if (select > 0) {
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey next = iterator.next();
                if (next.isReadable()) {
                    SocketChannel channel = (SocketChannel)next.channel();
                    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    int read = channel.read(byteBuffer);
                    String result = new String(byteBuffer.array());
                    System.out.println("msg is :" + result); //删除当前的selectionKey,防止重复操作
                }
                iterator.remove();
            }
        } else {

        }
    }

    public static void main(String[] args) {
        NIOChatClient nioChatClient = new NIOChatClient();
        new Thread() {
            @Override
            public void run() {

                while (true) {
                    try {
                        nioChatClient.readMsg();
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                try {
                    nioChatClient.sendMsg(line);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }





}
